//-----------------------有序uuid生成
let sequence = 0;
let lastPrefix = null;
let uuidArray = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

function transRadix(num, len) {
    let sb = '';
    do {
        let index = num % uuidArray.length;
        num = Math.floor(num / uuidArray.length);
        sb = uuidArray.substr(index, 1) + sb;
    } while (num > 0);
    for (let i = sb.length; i < len; i++) {
        sb = '0' + sb;
    }
    return sb;
}

function sequenceGen(prefix) {
    if (prefix != lastPrefix) {
        sequence = 0;
    }
    let seq = ++sequence;
    lastPrefix = transRadix(seq, 4);
    return lastPrefix;
}

//guid 生成
function guid() {
    let timestamp = new Date().getTime();
    let uuid = '';
    let prefix = transRadix(timestamp, 8);
    uuid = uuid + prefix;
    uuid = uuid + sequenceGen(prefix);
    for (let i = uuid.length; i < 32; i++) {
        uuid = uuid + uuidArray.substr(Math.floor(Math.random() * uuidArray.length), 1);
    }
    return uuid;
}
